﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class DepartmentObject
    {

        public static Department[] GetDepartments()
        {
            return Query(null);
        }


        public static Department GetDepartment(int id)
        {
            Department department = new Department();
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DepartmentDesc.Id,ConditionExpress.EQUALS,id);
            builder.QueryTableName = DepartmentDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable departments = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            department.Id              = Convert.ToInt32(departments.Rows[0][DepartmentDesc.Id]);
            department.Name            = Convert.ToString(departments.Rows[0][DepartmentDesc.Name]);
            return department;
        }

        public static Department GetDepartment(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DepartmentDesc.Name, ConditionExpress.EQUALS, name);

            Department[] set = Query(builder.ConditionString);

            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static Department[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + DepartmentDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data

            if (table != null && table.Rows.Count > 0)
            {
                Department[] departments = new Department[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    Department department = new Department();

                    department.Id      = Convert.ToInt32(table.Rows[i][DepartmentDesc.Id]);
                    department.Name    = Convert.ToString(table.Rows[i][DepartmentDesc.Name]);

                    departments.SetValue(department, i);
                }

                return departments;
            }
            return null;
        }

        public static Int32 Add(Department department)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(DepartmentDesc.Name,    department.Name);
            builder.QueryTableName = DepartmentDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);

        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(DepartmentDesc.Id,ConditionExpress.EQUALS,id);
            builder.QueryTableName = DepartmentDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(Department department)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(DepartmentDesc.Name,     department.Name);
            builder.ConditionAdd(DepartmentDesc.Id,ConditionExpress.EQUALS,department.Id);
            builder.QueryTableName = DepartmentDesc.TableName;

            string sql = builder.UpdateCommandLine;
            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

    }
}
